package com.banesco.precargadepositos.interfaz.componentes;

import java.util.Calendar;
import java.util.Date;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.container.HorizontalFieldManager;
import net.rim.device.api.ui.container.PopupScreen;

public class MyDateTimePicker extends PopupScreen {

	private int mMonth = 1;
	private int mYear = 2009;

	private NumScrollListField mMonthField;
	private NumScrollListField mYearField;
	Calendar calendar = Calendar.getInstance();

	public MyDateTimePicker(Date date) {
		super(new HorizontalFieldManager(), PopupScreen.DEFAULT_CLOSE);
		calendar.setTime(date);
		mYear = calendar.get(Calendar.YEAR);
		// Calendar.JANUARY == 0, so +1 value
		mMonth = calendar.get(Calendar.MONTH) + 1;
		

		mMonthField = new NumScrollListField(mMonth, 1, 12);
		add(mMonthField);
		
		mYearField = new NumScrollListField(mYear, 1990, 2099);
		add(mYearField);
	}

	protected boolean navigationClick(int status, int time) {
		return super.navigationClick(status, time);
	}

	public Date getDate() {
		return calendar.getTime();
	}

	public MyDateTimePicker() {
		this(Calendar.getInstance().getTime());
	}

	abstract class ScrollListField extends LabelField {
		int mWidth = 0;

		public ScrollListField() {
			super("", FOCUSABLE);
		}

		protected abstract void switchValue(int step);

		protected boolean navigationMovement(int dx, int dy, int status,
				int time) {
			if (Math.abs(dy) > Math.abs(dx)) {
				switchValue(dy);
				return true;
			} else
				return super.navigationMovement(dx, dy, status, time);
		}

		public int getPreferredWidth() {
			return mWidth;
		}

		public int getPreferredHeight() {
			return super.getPreferredHeight() + 24;
		}

		protected void layout(int width, int height) {
			super.layout(width, height);
			setExtent(getPreferredWidth(), getPreferredHeight());
		}

		public abstract int getValue();

		protected boolean navigationClick(int status, int time) {
			return super.navigationClick(status, time);
		}

	}

	class NumScrollListField extends ScrollListField {
		int mValue;
		int mMinValue;
		int mMaxValue;

		public NumScrollListField(int val, int minVal, int maxVal) {
			mValue = val;
			mMinValue = minVal;
			mMaxValue = maxVal;

			setText(String.valueOf(mValue));
			int maxDig = String.valueOf(mMaxValue).length();
			String test = String.valueOf(pow(10, maxDig));
			mWidth = getFont().getAdvance(test.substring(1));
			mWidth += 10;
		}

		int pow(int x, int y) /*
							 * we define the power method with base x and power
							 * y (i.e., x^y)
							 */
		{
			int z = x;

			for (int i = 1; i < y; i++)
				z *= x;

			return z;
		}

		protected void switchValue(int step) {
			int value = mValue + step;
			if (value > mMaxValue)
				value = value - (mMaxValue - mMinValue + 1);
			if (value < mMinValue)
				value = value + (mMaxValue - mMinValue + 1);
			setValue(value);
		}

		private void setValue(int value) {
			mValue = value;
			setText(String.valueOf(mValue));
		}

		public int getValue() {
			return mValue;
		}
	}
}
